﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;

namespace NewsSpider.App
{
    public partial class AddWebsiteFrm : Form
    {
        delegate void SetTextCallback(string text);
        private Thread catchThread = null;
        public string strShowLinks = "";

        NewsSpider.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\cache.ini");

        public AddWebsiteFrm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 显示实时消息到showMsg
        /// </summary>
        /// <param name="strMessage">实时消息</param>
        private void SetText(string strMessage)
        {
            if (this.showMsg.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { strMessage });
            }
            else
            {
                this.showMsg.Text = strMessage;
            }
        }

        private void AddWebsiteFrm_Load(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = NewsSpider.Utility.CommonHelper.GetCurrentSoftSkin();
            this.Icon = new Icon(NewsSpider.Utility.ICONHelper.SetFormICON());

            //读取列表Cache
            txtWebsiteURL.Text = iniFile.GetString("TestCatchList", "WebsiteURL", "");
            txtWebName.Text = iniFile.GetString("TestCatchList", "WebsiteName", "");
            txtListTagStart.Text = iniFile.GetString("TestCatchList", "ListTagStart", "");
            txtListTagEnd.Text = iniFile.GetString("TestCatchList", "ListTagEnd", "");
            txtLinkTagStart.Text = iniFile.GetString("TestCatchList", "LinkTagStart", "");
            txtLinkTagEnd.Text = iniFile.GetString("TestCatchList", "LinkTagEnd", "");

            //读取内容Cache
            txtTitleTagStart.Text = iniFile.GetString("TestCatchDetail", "TitleTagStart", "");
            txtTitleTagEnd.Text = iniFile.GetString("TestCatchDetail", "TitleTagEnd", "");
            txtContentTagStart.Text = iniFile.GetString("TestCatchDetail", "ContentTagStart", "");
            txtContentTagEnd.Text = iniFile.GetString("TestCatchDetail", "ContentTagEnd", "");
            txtAuthorTagStart.Text = iniFile.GetString("TestCatchDetail", "AuthorTagStart", "");
            txtAuthorTagEnd.Text = iniFile.GetString("TestCatchDetail", "AuthorTagEnd", "");
            txtSourceTagStart.Text = iniFile.GetString("TestCatchDetail", "SourceTagStart", "");
            txtSourceTagEnd.Text = iniFile.GetString("TestCatchDetail", "SourceTagEnd", "");

            btnTestCatchContent.Enabled = false;
        }

        private void btnTestCatchList_Click(object sender, EventArgs e)
        {
            btnTestCatchContent.Enabled = false;
            catchThread = new Thread(new ThreadStart(CatchNewsList));
            catchThread.Start();

            if (checkBox1.Checked)
            {
                //存储文章列表测试采集参数
                iniFile.WriteValue("TestCatchList", "WebsiteURL", txtWebsiteURL.Text.Trim());
                iniFile.WriteValue("TestCatchList", "WebsiteName", txtWebName.Text.Trim());
                iniFile.WriteValue("TestCatchList", "ListTagStart", txtListTagStart.Text.Trim());
                iniFile.WriteValue("TestCatchList", "ListTagEnd", txtListTagEnd.Text.Trim());
                iniFile.WriteValue("TestCatchList", "LinkTagStart", txtLinkTagStart.Text.Trim());
                iniFile.WriteValue("TestCatchList", "LinkTagEnd", txtLinkTagEnd.Text.Trim());
            }

            btnTestCatchContent.Enabled = true;
        }

        private void CatchNewsList()
        {
            string strGetListHtmlContent = "", strShowLinksList = "";

            //匹配列表区域
            string strURL = txtWebsiteURL.Text.Trim();
            NewsSpider.Utility.WebClient client = new Utility.WebClient();
            client.Encoding = System.Text.Encoding.GetEncoding(comboBox1.Text.Trim());
            client.OpenRead(strURL);
            string strGetHtmlContent = client.RespHtml;

            int k = 1;
            try
            {
                NewsSpider.Utility.PublicValue.SaveMessage(DateTime.Now.ToString("HH:mm:ss") + " 开始测试采集文章列表...");
                this.SetText(NewsSpider.Utility.PublicValue.GetMessage());

                Regex rx = new Regex(txtListTagStart.Text.Trim() + "(?<content>[\\s\\S]*)?" + txtListTagEnd.Text.Trim(), RegexOptions.Singleline);
                strGetListHtmlContent = rx.Match(strGetHtmlContent).Result("${content}");

                NewsSpider.Utility.PublicValue.SaveMessage(DateTime.Now.ToString("HH:mm:ss") + " 测试文章列表采集成功！");
                this.SetText(NewsSpider.Utility.PublicValue.GetMessage());
            }
            catch
            {
                MessageBox.Show("文章列表匹配失败！");
                NewsSpider.Utility.PublicValue.SaveMessage(DateTime.Now.ToString("HH:mm:ss") + " 文章列表采集失败！");
                this.SetText(NewsSpider.Utility.PublicValue.GetMessage());
                k++;
            }

            //匹配链接列表
            int a = 1;
            try
            {
                Regex clearString = new Regex(txtLinkTagStart.Text.Trim() + "(.+?)" + txtLinkTagEnd.Text.Trim(), RegexOptions.IgnoreCase | RegexOptions.Compiled);
                Match m = null;
                for (m = clearString.Match(strGetListHtmlContent); m.Success; m = m.NextMatch())
                {
                    string strGetLink = m.Groups[1].ToString().Trim();
                    strGetLink = strGetLink.Replace("\"", "").Trim();
                    strShowLinksList += strGetLink + "\r\n";

                    NewsSpider.Utility.PublicValue.SaveMessage(DateTime.Now.ToString("HH:mm:ss") + " 已采集到链接地址：" + strGetLink);
                    this.SetText(NewsSpider.Utility.PublicValue.GetMessage());

                    a++;
                }

                if (k == 1)
                {
                    webBrowser1.DocumentText = "<div style=\"padding: 5px;\">" + strShowLinksList + "</div>";
                    strShowLinks = strShowLinksList;
                }

                NewsSpider.Utility.PublicValue.SaveMessage(DateTime.Now.ToString("HH:mm:ss") + " 共采集到 " + a.ToString() + " 条链接地址！");
                this.SetText(NewsSpider.Utility.PublicValue.GetMessage());
            }
            catch
            {
                MessageBox.Show("文章列表中链接匹配失败！");
                NewsSpider.Utility.PublicValue.SaveMessage(DateTime.Now.ToString("HH:mm:ss") + " 文章列表中链接匹配失败！");
                this.SetText(NewsSpider.Utility.PublicValue.GetMessage());
            }

            btnTestCatchContent.Enabled = true;
        }

        private void btnTestCatchContent_Click(object sender, EventArgs e)
        {
            btnTestCatchContent.Enabled = false;
            catchThread = new Thread(new ThreadStart(CatchNewsContentByLinks));
            catchThread.Start();

            if (checkBox2.Checked)
            {
                //存储文章内容测试采集参数
                iniFile.WriteValue("TestCatchDetail", "TitleTagStart", txtTitleTagStart.Text.Trim());
                iniFile.WriteValue("TestCatchDetail", "TitleTagEnd", txtTitleTagEnd.Text.Trim());
                iniFile.WriteValue("TestCatchDetail", "ContentTagStart", txtContentTagStart.Text.Trim());
                iniFile.WriteValue("TestCatchDetail", "ContentTagEnd", txtContentTagEnd.Text.Trim());
                iniFile.WriteValue("TestCatchDetail", "AuthorTagStart", txtAuthorTagStart.Text.Trim());
                iniFile.WriteValue("TestCatchDetail", "AuthorTagEnd", txtAuthorTagEnd.Text.Trim());
                iniFile.WriteValue("TestCatchDetail", "SourceTagStart", txtSourceTagStart.Text.Trim());
                iniFile.WriteValue("TestCatchDetail", "SourceTagEnd", txtSourceTagEnd.Text.Trim());
            }
        }

        /// <summary>
        /// 获取链接地址列表
        /// </summary>
        private void CatchNewsContentByLinks()
        {
            string strGetLinksList = strShowLinks;
            strGetLinksList = NewsSpider.Utility.StringHelper.CleanHtml(strGetLinksList);
            strGetLinksList = strGetLinksList.Replace("\r\n", "|");
            string[] splitLinks = strGetLinksList.Split(new char[] { '|' });
            for (int i = 0; i < splitLinks.Length - 1; i++)
            {
                string strLink = splitLinks[i].ToString();
                NewsSpider.Utility.PublicValue.SaveMessage(DateTime.Now.ToString("HH:mm:ss") + " [" + i.ToString() + "]正在采集：" + strLink + " ，状态：" + CatchNewsContent(strLink));
                this.SetText(NewsSpider.Utility.PublicValue.GetMessage());
            }

            btnTestCatchContent.Enabled = true;
            btnAddWebsite.Enabled = true;
        }

        /// <summary>
        /// 根据链接地址采集内容
        /// </summary>
        /// <param name="strURL">链接地址</param>
        /// <returns></returns>
        private string CatchNewsContent(string strURL)
        {
            NewsSpider.Utility.WebClient client = new Utility.WebClient();
            client.Encoding = System.Text.Encoding.GetEncoding(comboBox1.Text.Trim());
            client.OpenRead(strURL);
            string strGetHtmlContent = client.RespHtml;

            string strReturnString = "", strTitle = "", strContent = "", strAuthor = "", strSource = "";

            //采集文章标题
            try
            {
                Regex rx = new Regex(txtTitleTagStart.Text.Trim() + "(?<content>[\\s\\S]*)?" + txtTitleTagEnd.Text.Trim(), RegexOptions.Singleline);
                strTitle = rx.Match(strGetHtmlContent).Result("${content}");
                strTitle = NewsSpider.Utility.StringHelper.CleanHtml(strTitle).Trim();
                strReturnString = "采集成功！";
            }
            catch
            {
                strReturnString = "文章标题采集失败！";
            }

            //采集文章内容
            try
            {
                Regex rx = new Regex(txtContentTagStart.Text.Trim() + "(?<content>[\\s\\S]*)?" + txtContentTagEnd.Text.Trim(), RegexOptions.Singleline);
                strContent = rx.Match(strGetHtmlContent).Result("${content}");
                //strContent = NewsSpider.Utility.PublicValue.CleanHtml(strContent).Trim();
                strReturnString = "采集成功！";
            }
            catch
            {
                strReturnString = "文章内容采集失败！";
            }

            //采集作者
            if (!string.IsNullOrEmpty(txtAuthorTagStart.Text.Trim()))
            {
                try
                {
                    Regex rx = new Regex(txtAuthorTagStart.Text.Trim() + "(?<content>[\\s\\S]*)?" + txtAuthorTagEnd.Text.Trim(), RegexOptions.Singleline);
                    strAuthor = rx.Match(strGetHtmlContent).Result("${content}");
                    strAuthor = NewsSpider.Utility.StringHelper.CleanHtml(strAuthor).Trim();
                    strReturnString = "采集成功！";
                }
                catch
                {
                    strReturnString = "文章作者采集失败！";
                }
            }

            //采集文章来源
            if (!string.IsNullOrEmpty(txtSourceTagStart.Text.Trim()))
            {
                try
                {
                    Regex rx = new Regex(txtSourceTagStart.Text.Trim() + "(?<content>[\\s\\S]*)?" + txtSourceTagEnd.Text.Trim(), RegexOptions.Singleline);
                    strSource = rx.Match(strGetHtmlContent).Result("${content}");
                    strSource = NewsSpider.Utility.StringHelper.CleanHtml(strSource).Trim();
                    strReturnString = "采集成功！";
                }
                catch
                {
                    strReturnString = "文章来源采集失败！";
                }

            }

            webBrowser1.DocumentText = "<h1>" + strTitle + "</h1><div><h5>" + strAuthor + "</h5><h5>" + strSource + "</h5></div><h4>" + strContent + "</h4>";

            return strReturnString;

        }


        private void btnAddWebsite_Click(object sender, EventArgs e)
        {
            string strSiteUrl = txtWebsiteURL.Text.Trim();
            NewsSpider.DAL.CatchWebsitesSQLiteDAL cwsdal = new DAL.CatchWebsitesSQLiteDAL();
            if (cwsdal.CheckArticleIsExist(strSiteUrl))
            {
                MessageBox.Show("站点已存在，请不要重复添加！");
            }
            else
            {
                NewsSpider.BLL.CatchWebsitesSQLiteBLL cwsbll = new BLL.CatchWebsitesSQLiteBLL();
                NewsSpider.Model.CatchWebsites cws = new Model.CatchWebsites();
                cws.SiteName = txtWebName.Text.Trim();
                cws.SiteUrl = strSiteUrl;
                cws.TagStartList = txtListTagStart.Text.Trim();
                cws.TagEndList = txtListTagEnd.Text.Trim();
                cws.TagLinkStart = txtLinkTagStart.Text.Trim();
                cws.TagLinkEnd = txtLinkTagEnd.Text.Trim();
                cws.TagStartTitle = txtTitleTagStart.Text.Trim();
                cws.TagEndTitle = txtTitleTagEnd.Text.Trim();
                cws.TagStartContent = txtContentTagStart.Text.Trim();
                cws.TagEndContent = txtContentTagEnd.Text.Trim();
                cws.TagStartAuthor = txtAuthorTagStart.Text.Trim();
                cws.TagEndAuthor = txtAuthorTagEnd.Text.Trim();
                cws.TagStartSource = txtSourceTagStart.Text.Trim();
                cws.TagEndSource = txtSourceTagEnd.Text.Trim();
                cwsbll.InsertCatchWebsitesBLL(cws);

                MessageBox.Show("添加成功！");
            }
        }

        private void txtWebsiteURL_TextChanged(object sender, EventArgs e)
        {
            string strURL = txtWebsiteURL.Text.Trim();
            if (string.IsNullOrEmpty(strURL))
            {
                MessageBox.Show("请输入采集网址！");
                txtWebsiteURL.Focus();
            }
            else
            {
                webBrowser1.Url = new Uri(strURL);

                //自动获取标题
                NewsSpider.Utility.WebClient client = new NewsSpider.Utility.WebClient();
                client.Encoding = System.Text.Encoding.GetEncoding("gb2312");
                client.OpenRead(strURL);
                string strGetHtmlContent = client.RespHtml;
                Regex rx = new Regex("<title>(?<content>[\\s\\S]*)?</title>", RegexOptions.Singleline);
                txtWebName.Text = rx.Match(strGetHtmlContent).Result("${content}");
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            txtWebsiteURL.Text = "";
            txtWebName.Text = "";
            txtListTagStart.Text = "";
            txtListTagEnd.Text = "";
            txtLinkTagStart.Text = "";
            txtLinkTagEnd.Text = "";
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            txtTitleTagStart.Text = "";
            txtTitleTagEnd.Text = "";
            txtContentTagStart.Text = "";
            txtContentTagEnd.Text = "";
            txtAuthorTagStart.Text = "";
            txtAuthorTagEnd.Text = "";
            txtSourceTagStart.Text = "";
            txtSourceTagEnd.Text = "";
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

       
    }
}
